package ecache

import "sync"

type LabelCacheMap struct {
	sync.RWMutex
	Data map[string]interface{}
}

var LabelCache *LabelCacheMap

func NewLabelCache() *LabelCacheMap {
	return &LabelCacheMap{
		Data: make(map[string]interface{}),
	}
}

func (this *LabelCacheMap) SetAll(m map[string]interface{}) {
	this.Lock()
	defer this.Unlock()
	this.Data = m
}

func (this *LabelCacheMap) GetByKey(key string) (interface{}, bool) {
	this.RLock()
	defer this.RUnlock()
	value, exists := this.Data[key]
	return value, exists
}

func (this *LabelCacheMap) SetByKey(key string, value interface{}) {
	this.RLock()
	defer this.RUnlock()
	if key == "" {
		return
	}
	this.Data[key] = value
}

func (this *LabelCacheMap) GetAll() map[string]interface{} {
	this.RLock()
	defer this.RUnlock()
	return this.Data
}
